package com.health.mapp.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.health.db.model.PatientShortInfo;
import com.health.db.service.user.IPatientShortInfoService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.QueryPatientListReq;
import com.health.mapp.bean.QueryPatientListResp;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

/**
 * 查询用户归属就诊人信息
 * @author yuchong
 */
public class QueryPatientListImpl implements ActionInterface{

	@Autowired
	IPatientShortInfoService patientShortInfoService;
	
    
    private static Logger logger = Logger.getLogger(QueryPatientListImpl.class);

    @Override
    public Response execute(Header header, Object reqBean) throws Exception {

    	QueryPatientListReq req = (QueryPatientListReq) reqBean;
    	QueryPatientListResp _resp = new QueryPatientListResp();

    	//userId 必填
        if (StringUtils.isEmpty(req.getUserId()))
        {
            _resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
            _resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);

            return _resp;
        }

        try {
					
		_resp.setUserId(req.getUserId());
		
		//查询所属所有就诊人信息
		 List<PatientShortInfo> patientList=patientShortInfoService.selectByUserId(Long.valueOf(req.getUserId()));
		 
		 if (patientList.size()>0) {
			 PatientShortInfo defalutPatient = patientList.get(0);
			 _resp.setDefalutPatient(defalutPatient);
		}else{
			 _resp.setDefalutPatient(new PatientShortInfo());
		}
		 _resp.setPatientList(patientList);
		 _resp.setPatientSize(patientList.size());
		 
		 _resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
		 _resp.setErrorMsg(ResultCode.OPERATION_SUCCESS_NOTE);
        	
        }catch (Exception e)
        {
            logger.error(e.getMessage());
            _resp.setErrorCode(-1);
            _resp.setErrorMsg("code error");
            logger.info("code error");
        }

        return _resp;
    }


}
